package com.hrzyit.whistle.cases.widgets

import android.content.Context
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import com.hrzyit.base.ext.onClick
import com.hrzyit.base.utils.AppPrefsUtils
import com.hrzyit.base.utils.RolePermissionUtils
import com.hrzyit.provider.common.ProviderConstant
import com.hrzyit.whistle.cases.R
import com.hrzyit.whistle.cases.data.bean.AuthMsgEntityVo
import com.hrzyit.whistle.cases.data.bean.UserDeptInfoVo
import com.hrzyit.whistle.cases.event.CaseAppendApproveEvent
import com.hrzyit.whistle.cases.presenter.CaseListPresenter
import com.hrzyit.whistle.cases.ui.adapter.ApproveContentListAdapter
import com.lxj.xpopup.animator.PopupAnimator
import com.lxj.xpopup.core.CenterPopupView
import kotlinx.android.synthetic.main.popup_case_accept.view.mCancelBtn
import kotlinx.android.synthetic.main.popup_case_accept.view.mConfirmBtn
import kotlinx.android.synthetic.main.popup_case_approve_view.view.*
import org.greenrobot.eventbus.EventBus

/**
 * 弹出窗口：批示详情界面
 * @author YangWei
 * @date 2019/4/21
 */
class CaseApproveViewPopup(context: Context, val presenter: CaseListPresenter) : CenterPopupView(context) {
    lateinit var authMsgEntityVo: AuthMsgEntityVo

    // 返回自定义弹窗的布局
    override fun getImplLayoutId(): Int {
        return R.layout.popup_case_approve_view
    }

    // 执行初始化操作，比如：findView，设置点击，或者任何你弹窗内的业务逻辑
    override fun onCreate() {
        super.onCreate()
        initView()
        mConfirmBtn.onClick {
            // 转到追加批示页面
            dismiss()
            val caseAppendApproveEvent = CaseAppendApproveEvent(authMsgEntityVo)
            EventBus.getDefault().post(caseAppendApproveEvent)

        }
        mCancelBtn.onClick {
            dismiss() // 关闭弹窗
        }
    }
    /**
     * 初始化批示信息
     */
    private fun initView(){
        // 初始设置批示信息
        val layoutManager = LinearLayoutManager(context)
        val approveContentListAdapter = ApproveContentListAdapter(context)
        mViewApproveContentRv.layoutManager = layoutManager
        mViewApproveContentRv.adapter = approveContentListAdapter
        approveContentListAdapter.setData(authMsgEntityVo.authMessageList!!)

        // 根据权限显示追加批示按钮
        when (RolePermissionUtils.isLeader()) {
            true -> {
                // 只有领导可以批示
                mConfirmBtn.visibility = View.VISIBLE
            }
        }

        // 初始化批示单位和人员信息
        val deptIdList = mutableListOf<Long>()
        val userIdList = mutableListOf<Long>()
        val deptNamesList = mutableListOf<String>()
        val userNamesList = mutableListOf<String>()
        val userDeptInfoVoList = mutableListOf<UserDeptInfoVo>()
        authMsgEntityVo.authReceiverVoList?.forEach {
            deptIdList.add(it.toDept)
            deptNamesList.add(it.toDeptName)
            it.toUserList.forEach { item ->
                userIdList.add(item.userId)
                userNamesList.add(item.userName)
                userDeptInfoVoList.add(item)
            }

        }
        authMsgEntityVo.receiveUserIds = userIdList
        authMsgEntityVo.receiveDeptIds = deptIdList
        authMsgEntityVo.userDeptInfoVoList = userDeptInfoVoList

        mReceiverDeptNamesTv.text = deptNamesList.joinToString("、")
        mReceiverUserNamesTv.text = userNamesList.joinToString("、")
    }

    // 设置最大宽度，看需要而定
    override fun getMaxWidth(): Int {
        return super.getMaxWidth()
    }

    // 设置最大高度，看需要而定
    override fun getMaxHeight(): Int {
        return super.getMaxHeight()
    }

    // 设置自定义动画器，看需要而定
    override fun getPopupAnimator(): PopupAnimator {
        return super.getPopupAnimator()
    }
}